package com.demo.repository.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.demo.repository.interceptor.DynamicDataSourceInterceptor;
import com.demo.repository.routing.Db1DatasourceRouting;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@Configuration
@MapperScan(basePackages = "com.demo.repository.db1.dao",sqlSessionFactoryRef = "sqlSessionFactoryDb1")
public class ConfigDb1 {

    @Value("${spring.datasource.db1.username}")
    private String username;
    @Value("${spring.datasource.db1.password}")
    private String password;
    @Value("${spring.datasource.db1.url}")

    private String url;
    @Value("${spring.datasource.db1.url_r}")
    private String url_r;
    @Value("${spring.datasource.db1.driver-class-name}")
    private String driverClass;


    /**
     *
     * @return
     */


    @Bean("db1")
    @Primary
    public DataSource getDb1Datasource() {
        DruidDataSource source = new DruidDataSource();
//      source.getConnection();
        source.setUsername(username);
        source.setPassword(password);
        source.setUrl(url);
        source.setDriverClassName(driverClass);


        DruidDataSource sourceR = new DruidDataSource();
        sourceR.setUsername(username);
        sourceR.setPassword(password);
        sourceR.setUrl(url_r);
        sourceR.setDriverClassName(driverClass);


        Map<Object,Object > dataSourceHashMap = new HashMap<>();
        dataSourceHashMap.put("db1", source);
        dataSourceHashMap.put("db1_r", sourceR);
        Db1DatasourceRouting db1DatasourceRouting = new Db1DatasourceRouting();
        db1DatasourceRouting.setTargetDataSources(dataSourceHashMap);
        db1DatasourceRouting.setDefaultTargetDataSource(source);
        return db1DatasourceRouting;
    }

    @Bean("sqlSessionFactoryDb1")
    @Primary
    public SqlSessionFactory sqlSessionFactoryDb1(@Qualifier("db1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setPlugins(new DynamicDataSourceInterceptor());
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/demo/repository/db1/mapper/*.xml"));
        return factoryBean.getObject();
    }

//    @Bean("sqlSessionTemplateDb1")
//    @Primary
//    public SqlSessionTemplate sqlSessionTemplateDb1(@Qualifier("sqlSessionFactoryDb1") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

}
